home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / fsdomain / fsdomain.c < prev    next >
C/C++ Source or Header  |  1992-03-12  |  9KB  |  306 lines

  1. /*
  2.  * fsdomain.c --
  3.  *
  4.  *     Read and possibly change the domain prefix of a file system.
  5.  *  
  6.  */
  7.  
  8. #ifndef lint
  9. static char rcsid[] = "$Header: /sprite/src/cmds/fsdomain/RCS/fsdomain.c,v 1.5 92/03/12 16:36:36 voelker Exp $";
  10. #endif not lint
  11.  
  12. #include "sprite.h"
  13. #include "sys/file.h"
  14. #include "stdio.h"
  15. #include "errno.h"
  16.  
  17. #include "disk.h"
  18. #include "option.h"
  19.  
  20. /*
  21.  * The size is declared in <kernel/lfsSuperBlock.h>, and is 64
  22.  * bytes.
  23.  */
  24. #define MAX_LFS_DOMAIN_PREFIX 63  
  25. /*
  26.  * OFS_SUMMARY_PREFIX_LENGTH is defined in <kernel/ofs.h>
  27.  */
  28. #define MAX_OFS_DOMAIN_PREFIX OFS_SUMMARY_PREFIX_LENGTH - 1
  29.  
  30. #define INV_DOMAIN -9999
  31. int     newDomainNum = INV_DOMAIN;
  32. int     newServerID = -1;
  33. Boolean previousCheckPoint = FALSE;
  34. Boolean printInfo = FALSE;
  35. char    *newDomain = NULL;
  36.  
  37. Option optionArray[] = {
  38.     {OPT_DOC, "", (Address)NIL,
  39.      "fsdomain device"},
  40.     {OPT_STRING, "name", (char *)&newDomain,
  41.          "New domain name"},
  42.     {OPT_INT, "d", (Address)&newDomainNum,
  43.      "New domain number"},
  44.     {OPT_INT, "id", (Address)&newServerID,
  45.      "New server ID"},
  46.     {OPT_TRUE, "print", (Address)&printInfo,
  47.      "Print out values of file system structures."},
  48.     {OPT_TRUE, "prev", (Address)&previousCheckPoint,
  49.      "Use the previous (older) checkpoint.  Useful for when the kernel\n\tand the disk get out of sync on LFS checkpoints."}
  50. };
  51.  
  52. static int numOptions = sizeof(optionArray) / sizeof(Option);
  53.  
  54. /*
  55.  *----------------------------------------------------------------------
  56.  *
  57.  * ChangeLfsDomainPrefix
  58.  *
  59.  *      Change the domain prefix of an LFS file system.  If
  60.  *      newDomain is NULL, the old domain prefix is printed to stdout.
  61.  *
  62.  * Results:
  63.  *      None.
  64.  *
  65.  * Side effects:
  66.  *    Writes the new domain prefix to the LFS file system on stream;
  67.  *      Prints to stdout.
  68.  *
  69.  *----------------------------------------------------------------------
  70.  */
  71. void
  72. ChangeLfsDomainPrefix(stream, labelPtr, deviceName, newDomain)
  73.     int stream;
  74.     Disk_Label *labelPtr;
  75.     char *deviceName;
  76.     char *newDomain;
  77. {
  78.     LfsCheckPointHdr *headerPtr;
  79.     int              len;
  80.     int              status;
  81.     int              area = -1;
  82.     Boolean          writeCheckPoint = FALSE;
  83.  
  84.     headerPtr = Disk_ReadLfsCheckPointHdr(stream, labelPtr, &area);
  85.     printf("Latest checkpoint is #%d", area);
  86.     if (previousCheckPoint == TRUE) {
  87.     area = (area == 0) ? 1 : 0;
  88.     printf("...using #%d instead.\n", area);
  89.     headerPtr = Disk_ReadLfsCheckPointHdr(stream, labelPtr, &area);
  90.     } else {
  91.     putchar('\n');
  92.     }
  93.     if (headerPtr == NULL || area < 0 || area > 1) {
  94.     return;
  95.     }
  96.     if (printInfo) {
  97.     LfsCheckPointTrailer *trailerPtr;
  98.  
  99.     printf("LFS Checkpoint Header on %s:\n\n", deviceName);
  100.     Disk_PrintLfsCheckPointHdr(headerPtr);
  101.     printf("\nLFS Checkpoint Regions on %s:\n\n", deviceName);
  102.     Disk_ForEachCheckPointRegion(headerPtr, 
  103.                      Disk_PrintLfsCheckPointRegion);
  104.     printf("\nLFS Checkpoint Trailer on %s:\n\n", deviceName);
  105.     trailerPtr = Disk_LfsCheckPointTrailer(headerPtr);
  106.     Disk_PrintLfsCheckPointTrailer(trailerPtr);
  107.     return;
  108.     }
  109.     if (newDomain != NULL) {
  110.     len = strlen(newDomain);
  111.     if (len > MAX_LFS_DOMAIN_PREFIX) {
  112.         printf("The LFS limits domain prefixes to %d characters.\n",
  113.            MAX_LFS_DOMAIN_PREFIX);
  114.         printf("%s is %d characters too long.\n", newDomain,
  115.            len - MAX_LFS_DOMAIN_PREFIX);
  116.         return;
  117.     }
  118.     sprintf(headerPtr->domainPrefix, "%s", newDomain); 
  119.     writeCheckPoint = TRUE;
  120.     }
  121.     if (newDomainNum != INV_DOMAIN) {
  122.     headerPtr->domainNumber = newDomainNum;
  123.     writeCheckPoint = TRUE;
  124.     }
  125.     if (newServerID > 0) {
  126.     headerPtr->serverID = newServerID;
  127.     writeCheckPoint = TRUE;
  128.     }
  129.     if (writeCheckPoint) {
  130.     status = Disk_WriteLfsCheckPointHdr(stream, headerPtr, area, 
  131.                         labelPtr); 
  132.     if (status != 0) {
  133.         fprintf(stderr, "fsdomain: could not write LFS checkpoint ");
  134.         fprintf(stderr, "header: %s\n", deviceName);
  135.         free((Address)headerPtr);
  136.         return;
  137.     }
  138.     }    
  139.     printf("%s:\t%s\n", deviceName, headerPtr->domainPrefix);
  140.     printf("Server ID:\t%d\n", headerPtr->serverID);
  141.     printf("Domain number:\t%d\n", headerPtr->domainNumber);
  142.     free((Address)headerPtr);
  143.     return;
  144. }
  145.  
  146. /*
  147.  *----------------------------------------------------------------------
  148.  *
  149.  * ChangeOfsDomainPrefix
  150.  *
  151.  *      Change the domain prefix of an OFS file system.  If
  152.  *      newDomain is NULL, the old domain prefix is printed to stdout.
  153.  *
  154.  * Results:
  155.  *      None.
  156.  *
  157.  * Side effects:
  158.  *    Writes the new domain prefix to the OFS file system on stream,
  159.  *      modifying the Ofs_SummaryInfo;
  160.  *      Prints to stdout.
  161.  *
  162.  *----------------------------------------------------------------------
  163.  */
  164. ChangeOfsDomainPrefix(stream, labelPtr, deviceName, newDomain)
  165.     int stream;
  166.     Disk_Label *labelPtr;
  167.     char *deviceName;
  168.     char *newDomain;
  169. {
  170.     Ofs_SummaryInfo  *summaryPtr;
  171.     Ofs_DomainHeader *domainPtr;
  172.     int              len;
  173.     int              status;
  174.     Boolean          writeSummary = FALSE;
  175.     Boolean          writeDomain = FALSE;
  176.  
  177.     summaryPtr = Disk_ReadSummaryInfo(stream, labelPtr);
  178.     if (summaryPtr == NULL) {
  179.     printf(stderr, "fsdomain: couldn't read Ofs_SummaryInfo\n");
  180.     return;
  181.     }
  182.     domainPtr = Disk_ReadDomainHeader(stream, labelPtr);
  183.     if (domainPtr == NULL) {
  184.     printf(stderr, "fsdomain: couldn't read Ofs_DomainHeader\n");
  185.     return;
  186.     }
  187.     if (printInfo) {
  188.     printf("OFS Domain Header on %s:\n\n", deviceName);
  189.     Disk_PrintDomainHeader(domainPtr);
  190.     printf("\nOFS Summary Info on %s:\n\n", deviceName);
  191.     Disk_PrintSummaryInfo(summaryPtr);
  192.     return;
  193.     }
  194.     if (newDomain != NULL) {
  195.     len = strlen(newDomain);
  196.     if (len > MAX_OFS_DOMAIN_PREFIX) {
  197.         printf("The OFS limits domain prefixes to %d characters.\n",
  198.            MAX_OFS_DOMAIN_PREFIX);
  199.         printf("%s is %d characters too long.\n", newDomain,
  200.            len - MAX_OFS_DOMAIN_PREFIX);
  201.         free((Address)summaryPtr);
  202.         return;
  203.     }
  204.     sprintf(summaryPtr->domainPrefix, "%s", newDomain);
  205.     writeSummary = TRUE;
  206.     }
  207.     if (newDomainNum != INV_DOMAIN) {
  208.     summaryPtr->domainNumber = newDomainNum;
  209.     writeSummary = TRUE;
  210.     }
  211.     if (newServerID > 0) {
  212.     domainPtr->device.serverID = newServerID;
  213.     writeDomain = TRUE;
  214.     }
  215.     if (writeSummary) {
  216.     status = Disk_WriteSummaryInfo(stream, labelPtr, summaryPtr);
  217.     if (status != 0) {
  218.         fprintf(stderr, "fsdomain: could not write OFS summary ");
  219.         fprintf(stderr, "sector: %s\n", deviceName);
  220.         free((Address)summaryPtr);
  221.         return;
  222.     }
  223.     }
  224.     if (writeDomain) {
  225.     status = Disk_WriteDomainHeader(stream, labelPtr, domainPtr);
  226.     if (status != 0) {
  227.         fprintf(stderr, "fsdomain: could not write OFS domain ");
  228.         fprintf(stderr, "header: %s\n", deviceName);
  229.         free((Address)summaryPtr);
  230.         return;
  231.     }
  232.     }
  233.     printf("%s:\t%s\n", deviceName, summaryPtr->domainPrefix);
  234.     printf("Server ID:\t%d\n", domainPtr->device.serverID);
  235.     printf("Domain number:\t%d\n", summaryPtr->domainNumber);
  236.     free((Address)summaryPtr);
  237.     return;
  238. }
  239.  
  240. /*
  241.  *----------------------------------------------------------------------
  242.  *
  243.  * main --
  244.  *
  245.  *      Open the device stream, read and possibly change the
  246.  *      domain prefix of the file system found on the stream.
  247.  *
  248.  * Results:
  249.  *      None.
  250.  *
  251.  * Side effects:
  252.  *    Prints to stdout.
  253.  *
  254.  *----------------------------------------------------------------------
  255.  */
  256. void
  257. main(argc, argv)
  258.     int argc;
  259.     char *argv[];
  260. {
  261.     int        stream;
  262.     int        fstype;
  263.     Disk_Label *labelPtr;
  264.     char       *deviceName;
  265.  
  266.     argc = Opt_Parse(argc, argv, optionArray, numOptions);
  267.     if (argc != 2) {
  268.     Opt_PrintUsage(argv[0], optionArray, numOptions);
  269.     exit(FAILURE);
  270.     }
  271.     deviceName = argv[1];
  272.     /*
  273.      * If there are no new values, then just report the current ones.
  274.      */
  275.     if (newDomain == NULL && newDomainNum == INV_DOMAIN && newServerID < 0) {
  276.     stream = open(deviceName, O_RDONLY, 0);
  277.     } else {
  278.     stream = open(deviceName, O_RDWR, 0);
  279.     }
  280.     if (stream < 0) {
  281.     perror("opening device");
  282.     exit(FAILURE);
  283.     }
  284.  
  285.     labelPtr = Disk_ReadLabel(stream);
  286.     if (labelPtr == NULL) {
  287.     printf("fsdomain: cannot find label on device %s.  ", deviceName);
  288.     printf("Is the label\non the disk of the correct type for");
  289.     printf("the machine being used?\n");
  290.     }
  291.  
  292.     fstype = Disk_HasFilesystem(stream, labelPtr);
  293.     switch (fstype) {
  294.     case DISK_HAS_OFS:
  295.     ChangeOfsDomainPrefix(stream, labelPtr, deviceName, newDomain);
  296.     break;
  297.     case DISK_HAS_LFS:
  298.     ChangeLfsDomainPrefix(stream, labelPtr, deviceName, newDomain);
  299.     break;
  300.     default:
  301.     printf("%s: no file system found.\n", deviceName);
  302.     break;
  303.     }
  304.     free((Address)labelPtr);
  305. }
  306.